package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.LikesBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import com.heima.utils.thread.WmThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LikesBeahaviorServiceImpl implements LikesBehaviorService {
    //如果 redis中key和value  已经明确知道都是字符串 推荐使用StringRedisTemplate   也可以用redisTemplate<String,String>
//    @Autowired
//    RedisTemplate<String,String> redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    KafkaTemplate<String, String> kafkaTemplate;

    @Override
    public ResponseResult likes(LikesBehaviorDto dto) {
        //1.校验
        if (dto==null || dto.getArticleId()==null || dto.getOperation()==null || dto.getOperation()<0 || dto.getOperation()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //校验是否登陆
        WmUser user = WmThreadLocalUtil.getUser();
        //user.getId()==0算游客 说明 app用户点击的不登录先看看，这种情况也不允许关注
        if(user==null || user.getId()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String userId = String.valueOf(user.getId());//当前登陆人id
        String articleId = String.valueOf(dto.getArticleId());

        //用户封装行为消息到kafka
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);

        //2.利用redis的模版类  操作redis   使用set结构保存  点赞记录
        //使用reids步骤  1.导依赖  spring-boot-starter-data-redis    2.yml文件中间配置redis的地址    3.可以注入RedisTemplate
        //LIKE-BEHAVIOR-xxxxxxxxxxx
        BoundSetOperations<String, String> operations = stringRedisTemplate.boundSetOps(BehaviorConstants.LIKE_BEHAVIOR + articleId);
        if(dto.getOperation()==0){//点赞行为
            Boolean member = operations.isMember(userId);
            if(member){//说明点赞时   该用户之前已经点过赞
                return ResponseResult.errorResult(AppHttpCodeEnum.CUO_TISHI,"请勿重复点赞");
            }
            operations.add(userId);
            mess.setAdd(1);//点赞
        }else{//取消赞
            Boolean member = operations.isMember(userId);
            if(!member){
                return ResponseResult.errorResult(AppHttpCodeEnum.CUO_TISHI,"没有点过赞，请先点赞");
            }
            operations.remove(userId);
            mess.setAdd(-1);
        }

        //发送行为消息到kafka
        log.info("发送点赞行为消息到kafka, {}", JSON.toJSONString(mess));
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));

        //3.返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
